<?php

namespace app\admin\controller;
use app\common\controller\AdminBase;
use app\common\model\Comment as CommentModel;
use app\common\model\Article;

class Comment extends AdminBase
{
    protected $commentmodel;

    protected function _initialize()
    {
        parent::_initialize();
        $this->commentmodel = new CommentModel();
    }

    public function index()
    {

        $tptc = $this->commentmodel->alias('c')
            ->join('article f', 'f.id=c.object_id')
            ->join('user m', 'm.id=c.user_id')
            ->field('c.*,f.title,m.username,m.uuid')->order('c.id desc')->paginate(15);
        $this->assign('tptc', $tptc);
        return view();
    }

    public function delete($id)
    {
        $info = $this->commentmodel->find($id);
        $setColumn='comment_num';
        if($info['parent_id']){
            $setColumn='reply_num';
        }
        (new Article())->where('id', $info['object_id'])->setDec($setColumn);

        if ($this->commentmodel->where('id',$id)->delete()) {
            return json(array('code' => 200, 'msg' => '删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '删除失败'));
        }
    }

    public function alldelete()
    {
        $params = input('post.');
        $ids=$params['ids'];
        foreach ($params['ids'] as $k => $v) {
            $info = $this->commentmodel->find($v);
            (new Article())->where('id', $info['object_id'])->setDec('reply_num');
        }
        $result = $this->commentmodel->where('id','in',$ids)->delete();
        if ($result) {
            return json(array('code' => 200, 'msg' => '删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '删除失败'));
        }
    }
}